package com.kili.cloud.shorturl.service.impl;

import com.kili.cloud.shorturl.service.AbstractShortUrl;
import com.kili.cloud.shorturl.service.CustomerService;
import com.kili.cloud.shorturl.service.ShortUrlService;
import com.kili.cloud.shorturl.service.UrlMapInfoService;
import com.kili.cloud.shorturl.vo.InputReturnVO;
import com.kili.cloud.shorturl.vo.InputUrlData;
import com.kili.cloud.shorturl.vo.ShortUrlParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @description: 链接转化为短链接的服务实现类
 * @author: tom.liu
 * @date: 2019/6/16 22:09
 */
@Service
public class ShortUrlServiceImpl implements ShortUrlService {
    private static Logger logger = LoggerFactory.getLogger(ShortUrlServiceImpl.class);

    @Autowired
    UrlMapInfoService urlMapInfoService;

    @Autowired
    CustomerService customerService;

    @Autowired
    BeanFactory beanFactory;


    @Override
    public InputReturnVO input(ShortUrlParam param, int visitType, Long shortUrlMaxInputCount, String shortUrlDomain) {
        Assert.notNull(param, "param is null");
        logger.info("正常模式：长链接转短链接，param={}", param);
        long startTime = System.currentTimeMillis();

        InputReturnVO returnVO = new InputReturnVO();

        //根据访问类型初始化处理类
        AbstractShortUrl shortUrl = shortUrlBeanFactory(visitType);
        shortUrl.getContext().setShortUrlDomain(shortUrlDomain);
        shortUrl.getContext().setShortUrlMaxInputCount(shortUrlMaxInputCount);
        shortUrl.getContext().setHashCode(param.getOriginalUrl().hashCode());

        String url = null;
        try {
            //处理调用业务逻辑
            url = shortUrl.input(param);
            returnVO.setCode(200);
            returnVO.setMessage("request success");
            InputUrlData data = new InputUrlData();
            returnVO.setData(data);
            data.setUrl(url);
        } catch (Exception e) {
            logger.info("请求异常：" + e.getMessage());
            returnVO.setCode(500);
            returnVO.setMessage("request failure：" + e.getMessage());
        }
        logger.info("得到的短链接url={}，耗时={}", url, String.format("%s毫秒", (System.currentTimeMillis() - startTime)));
        return returnVO;
    }

    @Override
    public String visit(String uri, String ip, Long shortUrlMaxVisitCount) {
        Assert.hasText(uri, "input url is empty");
        //uri统一转为小写
        uri = uri.toLowerCase();
        logger.info("短链接访问长链接，url={}", uri);
        long startTime = System.currentTimeMillis();
        AbstractShortUrl shortUrl = shortUrlBeanFactory(1);
        shortUrl.getContext().setShortUrlMaxVisitCount(shortUrlMaxVisitCount);
        String originalUrl = shortUrl.visit(uri, ip);
        logger.info("得到的原链接url={},耗时={}", originalUrl, String.format("%s毫秒", (System.currentTimeMillis() - startTime)));
        return originalUrl;
    }


    /**
     * 功能描述: 短链接服务处理对象工厂方法
     * @param: int 访问类型  1-正式版  其他-试用版
     * @return: AbstractShortUrl
     * @author: tom.liu
     * @date: 2019/6/17 10:29
     */
    private AbstractShortUrl shortUrlBeanFactory(int visitType){
        AbstractShortUrl shortUrl;
        if(visitType == 1){
            shortUrl = beanFactory.getBean(NormalShortUrl.class);
        }else{
            shortUrl = beanFactory.getBean(SimpleShortUrl.class);
        }
        return shortUrl;
    }

}
